package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.GetDatumAdapter;
import com.wnhz.work.yz.bean.FeedBackBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---资料领取
 */
public class GetDatumActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private TextView tv_right;

    private ListView my_listView;
    private GetDatumAdapter mAdapter;
    private List<FeedBackBean.InfoEntity> info1 = new ArrayList<>();

    private void onUpLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        showDialog();
        XUtil.Post(Url.INFORMATION_FEEDBACKDATALIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        FeedBackBean bean = gson.fromJson(result, FeedBackBean.class);
                        info1 = bean.getInfo();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                initTitle();
                initView();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_datum);
        info1 = (List<FeedBackBean.InfoEntity>) getIntent().getSerializableExtra("info1");

        initData();
    }

    private void initData() {
        if (info1.size() == 0) {
            onUpLoad();
        } else {

            initTitle();
            initView();
        }
    }

    private void initTitle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("资料领取");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.submit).setOnClickListener(this);
        tv_right = (TextView) findViewById(R.id.submit);
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("保存");
        tv_right.setTextColor(0xff008c99);
    }

    private void initView() {
        my_listView = (ListView) findViewById(R.id.my_listView);
        mAdapter = new GetDatumAdapter(this, info1, this);
        my_listView.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        int index;
        int num;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //保存
            case R.id.submit:
                // MyToast("保存");
                Intent intent1 = new Intent();
                ArrayList<FeedBackBean.InfoEntity> info1 = (ArrayList<FeedBackBean.InfoEntity>) this.info1;
//                MyToast("" + info1.size());
                intent1.putExtra("info1", info1);
                setResult(RESULT_OK, intent1);
                finish();
                break;

            //加
            case R.id.iv_add:
                index = (int) v.getTag();
                num = Integer.parseInt(this.info1.get(index).getData_nums());
                num++;
                this.info1.get(index).setData_nums(num + "");
                mAdapter.notifyDataSetChanged();
                break;

            //减
            case R.id.reduce:
                index = (int) v.getTag();
                num = Integer.parseInt(this.info1.get(index).getData_nums());
                if (num > 1) {
                    num--;
                    this.info1.get(index).setData_nums(num + "");
                } else {
                    MyToast("再减就没了");
                }
                mAdapter.notifyDataSetChanged();
                break;


            default:
                break;
        }
    }
}
